<?php
!defined('P_W') && exit('Forbidden');
require_once MODE . '/base/frontcontroller.php';

class ManageController extends FrontController {
	var $_layoutFile = 'manage';
	var $_seopage;
	var $_defaultSeo;
	var $_seoname;
	var $_manageHelper;
	var $_expiredService;
	
	function ajax(){
		global $winduid;
		define('AJAX','1');
		if (!$winduid) Error::showError('您没有权限进入该页面',1); 
		list($part,$id) = $this->_gp(array('part','id'));
		include(D_P.'data/bbscache/house_config.php');
		$this->_viewer->permit = html_entity_decode($house_permit);	
		$this->_viewer->part = $part;
		$this->_setLayoutFile('ajax');
		$this->_setLayoutType('ajax');
		if(in_array($part,array('subplate'))){
			list($this->_viewer->checkplate) = $this->_gp(array('checkplate'));
			$housefieldsService = House::loadService('HouseFieldsService');/* @var $housefieldsService PW_HouseFieldsService*/;
			$this->_viewer->plate = $housefieldsService->getPlateByAreaId($id);
			if(!$this->_viewer->plate) echo 0;
			$this->_render();
			ajax_footer();
		}
	}

	function run() {
		if (!checkBroker($this->_viewer->userInfo['uid'])) {
			$this->_setTemplate('manage.sale');
			$this->sale();
			return true;
		}
		$this->_viewer->userInfo['business'] == 'sale' ? ($this->_setTemplate('manage.sale') || $this->sale()) : ($this->_setTemplate('manage.hire') || $this->hire());
	}
	
	/**
	 * 二手房选项卡
	 */
	function sale() {
		list($page,$type) = GP(array('page','type'));
		if (!$type || !in_array($type,array('publish','prepare','expired'))) $type = 'publish';
		$page = (int) $page < 1 ? 1 : (int) $page;
		$offset = 8;
		$scondSaleService = House::loadService('secondsaleservice');
		$extend = array('uid'=>$this->_viewer->userInfo['uid']);
		if ($type == 'publish') {
			$extend['state'] = 0;
		} elseif ($type == 'prepare') {
			$extend['state'] = 1;
		} else {
			$extend['state'] = 2;
		}
		$extend['uid'] = $GLOBALS['winduid'];
		list($total, $this->_viewer->result) = $scondSaleService->getDetailList(array(),'','','',$page,$offset,$extend);
		$resultHouseids = array();
		foreach ($this->_viewer->result as $key => $value) {
			$resultHouseids[] = $value['houseid'];	
		}
		$this->_viewer->topState = $this->_expiredService->getsExpire($resultHouseids, 'sale', 'top');
		$this->_viewer->urgentState = $this->_expiredService->getsExpire($resultHouseids, 'sale', 'urgent');
		$this->_viewer->expiredTime = $this->_expiredService->getsExpire($resultHouseids, 'sale', 'expired');
		$this->_viewer->pages = numofpage($total,$page,ceil($total/$offset),$this->_viewer->baseUrl.'a=sale&type='.$type.'&');
		$this->_viewer->current[$type] = 'class="current"';
		$this->_viewer->type = $type;
		$this->_viewer->right = $this->_getRight();
		$this->_viewer->actionCurrent['sale'] = 'class="current"';
	}
	
	/**
	 * 租房选项卡
	 */
	function hire() {
		list($page,$type) = GP(array('page','type'));
		if (!$type || !in_array($type,array('publish','prepare','expired'))) $type = 'publish';
		$page = (int) $page < 1 ? 1 : (int) $page;
		$offset = 8;
		$scondHireService = House::loadService('secondhireservice');
		$extend = array('uid'=>$this->_viewer->userInfo['uid']);
		if ($type == 'publish') {
			$extend['state'] = 0;
		} elseif ($type == 'prepare') {
			$extend['state'] = 1;
		} else {
			$extend['state'] = 2;
		}
		$extend['uid'] = $GLOBALS['winduid'];
		$this->_viewer->right = $this->_getRight();
		list($total, $this->_viewer->result) = $scondHireService->getDetailList(array(),'','','',$page,$offset,$extend);
		$resultHouseids = array();
		foreach ($this->_viewer->result as $key => $value) {
			$resultHouseids[] = $value['houseid'];	
		}
		$this->_viewer->topState = $this->_expiredService->getsExpire($resultHouseids, 'hire', 'top');
		$this->_viewer->urgentState = $this->_expiredService->getsExpire($resultHouseids, 'hire', 'urgent');
		$this->_viewer->expiredTime = $this->_expiredService->getsExpire($resultHouseids, 'hire', 'expired');
		$this->_viewer->pages = numofpage($total,$page,ceil($total/$offset),$this->_viewer->baseUrl.'a=hire&type=' . $type . '&');
		$this->_viewer->current[$type] = 'class="current"';
		$this->_viewer->type = $type;
		$this->_viewer->actionCurrent['hire'] = 'class="current"';
	}
	
	/**
	 * 删除房源
	 */
	function delete() {
		list($houses, $link) = GP(array('houses', 'link'));
		$this->_manageHelper->delete($houses, $link);
	}
	
	/**
	 * 房源下架
	 */
	function except() {
		list($houses, $link) = GP(array('houses', 'link'));
		$this->_manageHelper->except($houses, $link);
	}
	
	/**
	 * 房源上架
	 */
	function uphouse() {
		list($houses, $link) = GP(array('houses', 'link'));
		$this->_manageHelper->uphouse($houses, $link);
	}

	/**
	 * 刷新房源
	 */
	function refresh() {
		list($houseid, $link) = GP(array('houseid', 'link'),'',2);
		$this->_manageHelper->refresh($houseid, $link);
	}
	
	/**
	 * 加急房源交互
	 */
	function urgent() {
		global $winduid;
		define('AJAX',1);
		list($houseid, $link) = GP(array('houseid', 'link'));
		!$link && $link = 'sale';
		$result = $this->_manageHelper->urgent($houseid, $link);
		list($this->_viewer->info, $this->_viewer->house_rapidprice, $this->_viewer->house_rapidcredit, $this->_viewer->myCreditNum, $this->_viewer->expiredTime) = $result;
		$link == 'hire' && $this->_viewer->link = 'hire';
		isHouseManager($winduid, $link) && $this->_viewer->isHouseManager = 1;
		$this->_viewer->houseid = $houseid;
		$this->_setLayoutFile('ajax');
		$this->_setLayoutType('ajax');
	}
	
	/**
	 * 加急房源处理
	 */
	function dourgent() {
		define('AJAX',1);
		list($houseid, $days, $link, $isurgent, $isback, $ismsg) = GP(array('houseid','days', 'link', 'isurgent' ,'isback', 'ismsg'),'',2);
		$this->_manageHelper->dourgent($houseid, $days, $link, $isurgent, $isback, $ismsg);
	}
	
	/**
	 * 置顶交互
	 */
	function top() {
		global $winduid;
		define('AJAX',1);
		list($houseid, $link) = GP(array('houseid', 'link'));
		!$link && $link = 'sale';
		$result = $this->_manageHelper->top($houseid, $link);
		list($this->_viewer->info, $this->_viewer->house_topprice, $this->_viewer->house_topcredit, $this->_viewer->myCreditNum, $this->_viewer->expiredTime) = $result;
		$link == 'hire' && $this->_viewer->link = 'hire';
		isHouseManager($winduid, $link) && $this->_viewer->isHouseManager = 1;
		$this->_viewer->houseid = $houseid;
		$this->_setLayoutFile('ajax');
		$this->_setLayoutType('ajax');
	}
	
	/**
	 * 处理置顶
	 */
	function dotop() {
		define('AJAX',1);
		list($houseid,$days, $link, $istop, $isback, $ismsg) = GP(array('houseid','days', 'link', 'istop' ,'isback', 'ismsg'),'',2);
		$this->_manageHelper->dotop($houseid, $days, $link, $istop, $isback, $ismsg);
	}
	
	/**
	 * 基本信息设置
	 */
	function setbroker() {
		global $winduid, $house_field;
		if (!checkBroker($this->_viewer->userInfo['uid'])) Error::showError('您没有权限进入该页面'); 
		$brokerService = House::loadService('brokerService');
		$brokerGroupService = House::loadService('BrokerGroupService');
		$brokerInfo = $brokerService->getBroker($winduid);
		$brokerInfo['announce'] = str_replace('<br />', '', $brokerInfo['announce']);
		$this->_viewer->baseurl = 'index.php?m=house&q=manage&a=editbroker';
		$brokerInfo['business'] == 'sale' ? $select_sale = 'checked = "checked"' : $select_hire = 'checked = "checked"';
		$this->_viewer->brokerInfo = $brokerInfo;
		$this->_viewer->brokerRight = $brokerGroupService->getBrokerGroup($brokerInfo['groupid']);
		$this->_viewer->select_sale = $select_sale;
		$this->_viewer->select_hire = $select_hire;
		$this->_viewer->house_field = $house_field;	
		$this->_viewer->plate = $house_field['plate'];
		$this->_viewer->actionCurrent['broker'] = 'class="current"';
	}
	
	/**
	 * 编辑基本信息
	 */
	function editbroker() {
		global $winduid;
		$info = GP(array('business','plate','company','address','email','tel','phone','announce'));
		$brokerService = House::loadService('brokerService');
		$brokerModule = $brokerService->getBrokerModule($winduid);
		$brokerModule->setBusiness($info[0]);
		count($info[1]) > 3 && Error::addError('服务商圈不能超过3个');
		(!$info[5] && !$info[6]) && Error::addError('固定电话与手机号码至少填写一项');
		$brokerModule->setPlate($info[1]);
		$brokerModule->setCompany($info[2]);
		$brokerModule->setAddress($info[3]);
		$brokerModule->setTel($info[5]);
		$brokerModule->setPhone($info[6]);
		$brokerModule->setAnnounce($info[7]);
		Error::checkError();
		$brokerService->editBroker($brokerModule);
		refreshto($this->_viewer->baseUrl.'a=setbroker','保存成功');
	}
	
	function addsale(){	
		list($info,$descrip,$step,$houseid,$from) = GP(array('info','atc_content','step','houseid','from'));
		$houseSaleHelper = House::loadClass('housesalehelper', 'helper');
		$this->_viewer = $houseSaleHelper->initSaleViewParams($info,$from,$houseid);
		if($step == 2){
			!$info['item'] && Showmsg('请同意房源发布规范');
			$info['descrip'] = $descrip;
			if($houseid){
				$houseSaleHelper->editHouseSale($info,$this->_viewer->ifbroker,$houseid);
				refreshto($houseSaleHelper->getSaleFromUrl($info['from'],$houseid),"编辑出售房源成功");
			}else{
				$houseSaleHelper->addHouseSale($info,$this->_viewer->ifbroker);
				refreshto($houseSaleHelper->getSaleFromUrl(),"发布出售房源成功");
			}
		}else{
			$this->_initViewData($this->_viewer->houseSaleModule);
		}
	}
	
	function addhire(){
		list($info,$descrip,$step,$houseid,$from) = GP(array('info','atc_content','step','houseid','from'));
		$houseHireHelper = House::loadClass('househirehelper', 'helper');
		$this->_viewer = $houseHireHelper->initHireViewParams($info,$from,$houseid);
		if($step == 2){
			!$info['item'] && Showmsg('请同意房源发布规范');
			$info['descrip'] = $descrip;
			if($houseid){
				$houseHireHelper->editHouseHire($info,$this->_viewer->ifbroker,$houseid);
				refreshto($houseHireHelper->getHireFromUrl($info['from'],$houseid),"编辑出租房源成功");
			}else{
				$houseHireHelper->addHire($info,$this->_viewer->ifbroker);
				refreshto($houseHireHelper->getHireFromUrl(),"发布出租房源成功");
			}
		}else{
			$this->_initViewData($this->_viewer->houseHireModule);
		}
	}
	
	function error(){}
	
	function _initViewData($module){
		$housefieldsService = House::loadService('HouseFieldsService');/* @var $housefieldsService PW_HouseFieldsService*/;
		$viewUtility= House::loadClass('houseview', 'utility');
		$this->_viewer->area = $housefieldsService->getCompsiteFieldsByType('area');//所属区域
		$this->_viewer->plate = $housefieldsService->getPlateByAreaId($module->areatype);//所在板块
		$this->_viewer->propertyright = $housefieldsService->getCompsiteFieldsByType('propertyright');//房屋产权
		$this->_viewer->house = $housefieldsService->getCompsiteFieldsByType('property');//房屋类型	
		$this->_viewer->fitment = $housefieldsService->getCompsiteFieldsByType('paystatus');//交互标准
		$this->_viewer->amenities = $housefieldsService->getCompsiteFieldsByType('amenities');//配套设实	
		$this->_viewer->buildtime = $viewUtility->buildYear();
		$this->_viewer->buildroom = $viewUtility->roomMap();
		$this->_viewer->buildhire = $viewUtility->hireMap();
		$this->_viewer->buildcharge = $viewUtility->chargeMap();
	}
	
	function _before() {
		global $winduid;
		if (!$winduid) Error::showError('您没有权限进入该页面'); 
		$this->_viewer->baseUrl = BASEURL.'q=manage&';
		$this->_manageHelper = House::loadClass('managehelper', 'helper');
		$this->_expiredService = House::loadService('expiredService');
		return true;
	}
	
	function _after(){
		houseSeoSettings($this->_seopage,$this->_defaultSeo,$this->_seoname);
	}
	
	function _getRight(){
		include(D_P.'data/bbscache/house_config.php');
		return array('urgent' => $house_rapidifopen,
					'top' => $house_topifopen);
	}
}